package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"mall/app/coupon/cmd/rpc/internal/svc"
	"mall/app/coupon/cmd/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetLatest3DaySessionLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetLatest3DaySessionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLatest3DaySessionLogic {
	return &GetLatest3DaySessionLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetLatest3DaySessionLogic) GetLatest3DaySession(in *pb.GetLatest3DaySessionReq) (*pb.GetLatest3DaySessionResp, error) {
	list, err := l.svcCtx.SeckillSessionModel.FindAll(l.ctx)
	if err != nil {
		l.Error(err)
		return nil, err
	}
	result := new(pb.GetLatest3DaySessionResp)
	for _, session := range list {
		tmp := pb.SeckillSession{}
		_ = copier.Copy(&tmp, &session)
		tmp.StartTime = session.StartTime.Format("2006-01-02 15:04:05")
		tmp.EndTime = session.EndTime.Format("2006-01-02 15:04:05")
		tmp.CreateTime = session.CreateTime.Format("2006-01-02 15:04:05")

		id := session.Id
		relations, err := l.svcCtx.SeckillRalationModel.FindBySessionId(l.ctx, id)
		if err != nil {
			l.Error(err)
			return nil, err
		}
		var r []*pb.SkuRelation
		_ = copier.Copy(&r, relations)

		tmp.List = r
		result.List = append(result.List, &tmp)
	}
	return result, nil
}
